package com.itwn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.bean.App;
import com.itwn.bean.AppPlatform;
import com.itwn.bean.AppType;
import com.itwn.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @ClassName: AppController
 * @Author: 流光亦断
 * @Date: 2021/8/20 16:11
 * @Version: 1.0     // 版本
 */
@RestController
public class AppController {

    @Autowired
    private AppService appService;
    private int page = 1;
    private int lastPage = 0;
    @RequestMapping("/queryApp")
    public PageInfo<App> queryApp(App app, String nowPage, int pageCout){
        if(nowPage!=null && !"".equals(nowPage)){
            int num=Integer.parseInt(nowPage);
            if (num < 0) {//说明点的是上一页或下一页
                if (num == -1 && page > 1) { //上一页
                    page--;
                }
                if (num == -2 && page < lastPage) {
                    page++;
                }
            } else {
                page = num;
            }
        }
        PageHelper.startPage(page,pageCout);
        List<App> list = appService.findCondition(app);
        PageInfo<App> info = new PageInfo<>(list);
        lastPage = info.getNavigateLastPage();
        return info;
    }


    @RequestMapping("/queryAppType")
    public List<AppType> findAllT(){
        return appService.findAllT();
    }

    @RequestMapping("/queryAppPlatform")
    public List<AppPlatform> findAllP(){
        return appService.findAllP();
    }

   @RequestMapping("/deleteApp")
    public String deleteApp(int id){
       int rows = appService.deleteById(id);
       if(rows>0){
        return "ok";
       }
       return "no";
   }
   @RequestMapping("/insertApp")
    public String addApp(App app){
       int rows  = appService.addApp(app);
       if(rows>0){
           return "ok";
       }
       return "no";
   }
   @RequestMapping("/updateApp")
    public String updateApp(App app){
       int rows = appService.updateApp(app);
       if(rows>0){
           return "ok";
       }
       return "no";
   }
   @RequestMapping("/deleteManyApp")
    public String deleteManyApp(int [] arr){
       int rows = appService.deleteManyApp(arr);
       if(rows>0){
           return "ok";
       }
       return "no";
   }
   @RequestMapping("/updateAppgo")
   public String updateAppgo(int id,int goId){
       App app = new App();
       app.setId(id);
       app.setAppStatus(goId);
       int rows = appService.updateApp(app);
       if(rows>0){
           return "ok";
       }
       return "no";
   }
}

